<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */
require_once ROOT_PATH.'/Abstract/base_abstract.php';

class Action extends base_abstract
{
    function index()
    {
        if (isset($_SESSION['user']['id'])) {
            $this->redirect('index.html');
        } else {
            $this->redirect('bind.html?url=login/bind.html');
        }
    }

    function bind()
    {
        if (isset($_SESSION['user']['id'])) {
            $this->redirect('index.html');
        }
        $api = new ApiSina();
        $keys = array(
            'code' => _GET('code'),
            'redirect_uri' => 'http://weiboapp.geek-zoo.com/login/bind.html'
        );
        if (!$key = $api->callback($keys)) {
            $this->msg('出错,请重试', 'login.html');
        }
        // print_r($key);exit;
        // 判断是否绑定过
        if ( !_model('account')->read(array('id'=>$key['id'])) ) {
            _model('account')->create(array(
                'id' => $key['id'],
                'access_token' => $key['access_token'],
                'is_bind' => 1
            ));
        } else {
            $a = _model('account')->update(array('id'=>$key['id']), array('access_token' => $key['access_token']));
        }
        
        // 判断用户是否拥有平台帐号
        if(!$info = _model('user')->read(array('account_id'=>$key['id']))){
            //写入user表
            $info['account_id'] = $key['id'];
            $info['add_time'] = time();
            $info['parent_id'] = 0;
            $info['followers_count'] = 0;
            $info['friends_count'] = 0;
            $info['id'] = _model('user')->create($info);
        }
        
        // print_r($api->home_timeline($key['access_token']));exit;

        $_SESSION['user'] = $info;
        $this->redirect('index.html');
    }
}

?>
